//
//  UPPersonalListView.m
//  Uang Pasti
//
//  Created by 李永彬 on 2025/8/27.
//

#import "UPPersonalListView.h"
#import "UPCustomTextFieldCell.h"

@interface UPPersonalListView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIImageView *containerImageView;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation UPPersonalListView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInitSubviews];
    }
    return self;
}

- (void)commonInitSubviews {
    [self.containerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self);
        make.bottom.top.equalTo(self).inset(20);
    }];
}

-(void)reloadData {
    [self.tableView reloadData];
}

- (void)setDataSources:(NSArray<PersonalBlackModel *> *)dataSources {
    _dataSources = dataSources;
    [self.tableView reloadData];
}

- (UIImageView *)containerImageView {
    if (!_containerImageView) {
        _containerImageView = [[UIImageView alloc] init];
        _containerImageView.contentMode = UIViewContentModeScaleAspectFit;
        _containerImageView.image = [UIImage imageNamed:@"presonal_list_bg"];
        _containerImageView.userInteractionEnabled = YES;
        [self addSubview:_containerImageView];
    }
    return _containerImageView;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        [_tableView registerClass:[UPCustomTextFieldCell class] forCellReuseIdentifier:@"UPCustomTextFieldCell"];
        [_tableView setShouldIgnoreScrollingAdjustment:YES];
        [_tableView setShouldRestoreScrollViewContentOffset:true];
        [self addSubview:_tableView];
    }
    return _tableView;
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSources.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UPCustomTextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UPCustomTextFieldCell" forIndexPath:indexPath];
    PersonalBlackModel *model = self.dataSources[indexPath.row];
    __block typeof(self) weakSelf = self;
    cell.arrowButtonBlock = ^(PersonalBlackModel * _Nullable model, BOOL isAddress) {
        if (weakSelf.cellArrowButtonBlock) {
            weakSelf.cellArrowButtonBlock(model, isAddress);
        }
    };
    cell.textFieldChangedBlock = self.cellTextFieldChangedBlock;
    cell.model = model;
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 75;
}

@end
